package com.dgut.service.impl;

import com.dgut.dao.AccountDao;
import com.dgut.domain.Account;
import com.dgut.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;


    public Account findById(Integer id) {
        return accountDao.findById(id);
    }

    public void trans(String from, String to, Float money) {
        //1.查找转账人是否存在
        Account fromAccount = accountDao.findByName(from);
        if (fromAccount==null){
            System.out.println("转账人不存在，请检查账户...");
            return;
        }
        //2.查找接收人是否存在
        Account toAccount = accountDao.findByName(to);
        if (toAccount==null){
            System.out.println("接收人不存在，请检查账户...");
            return;
        }
        //3.转账
        //扣款
        fromAccount.setMoney(fromAccount.getMoney()-money);
        accountDao.updateAccount(fromAccount);

        //手动制作一个异常
        int i=1/0;

        //进账
        toAccount.setMoney(toAccount.getMoney()+money);
        accountDao.updateAccount(toAccount);
    }

    public static void main(String[] args) {
//        org.springframework.transaction.TransactionDefinition
    }
}
